package gameengine.com;

import gameengine.com.actor.Creature;
import gameengine.com.state.StateGameplay;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.util.Log;

public class Map
{
	public static int MAP_COLUMNS = 6;
	public static int MAP_ROWS = 6;
	public static int MAP_CELL_WIDTH = 100; //= 2*tileset width
	public static int MAP_CELL_HEIGHT = 84;//= 2*tileset height
	public int[][] arrayMap;
	public int doortype = 0;
	public int doorExit_Row = 0;
	public int doorExit_Col = 0;

	public void draw(Canvas c)
	{
		MainActivity.mainPaint.setColor(Color.GREEN);
		MainActivity.mainPaint.setStyle(Style.STROKE);
		drawDoorExit(c);
		for (int i = 0; i < MAP_ROWS; i++) {
			if (StateGameplay.explorer.m_pos_rows == i)
				StateGameplay.explorer.draw(c);
			for (int n = 0; n < StateGameplay.creature.size(); n++) {
				if (StateGameplay.creature.get(n).m_pos_rows == i)
					StateGameplay.creature.get(n).draw(c);
			}
			for (int j = 0; j < MAP_COLUMNS; j++) {

				//	c.drawRect(j * MAP_CELL_WIDTH, i * MAP_CELL_HEIGHT, j * MAP_CELL_WIDTH + MAP_CELL_WIDTH, i * MAP_CELL_HEIGHT + MAP_CELL_HEIGHT, MainActivity.mainPaint);
				drawWall(c, i, j);
				//check and draw object

			}
		}
		
	}

	public void drawWall(Canvas c, int x, int y)
	{
		if ((arrayMap[x][y] & 1) == ArrayMap.TYPE_WALL_TOP) { // = 1
			StateGameplay.spriteWall.drawAFrame(c, 2, y * MAP_CELL_WIDTH, x * MAP_CELL_HEIGHT - 3);
			//	MainActivity.mainPaint.setColor(Color.YELLOW);
			//	c.drawRect(y * MAP_CELL_WIDTH, x * MAP_CELL_HEIGHT - 3, y * MAP_CELL_WIDTH + MAP_CELL_WIDTH, x * MAP_CELL_HEIGHT + 3, MainActivity.mainPaint);
			//	MainActivity.mainPaint.setColor(Color.GREEN);
		}
		//if ((arrayMap[x][y] & 2) == ArrayMap.TYPE_WALL_BOTTOM) { // = 2
		//	StateGameplay.spriteWall.drawAFrame(c, 0, y * MAP_CELL_WIDTH,(x + 1) * MAP_CELL_HEIGHT - 3);
		//	MainActivity.mainPaint.setColor(Color.YELLOW);
		//	c.drawRect(y * MAP_CELL_WIDTH, (x + 1) * MAP_CELL_HEIGHT - 3, y * MAP_CELL_WIDTH + MAP_CELL_WIDTH, (x + 1) * MAP_CELL_HEIGHT + 3, MainActivity.mainPaint);
		//	MainActivity.mainPaint.setColor(Color.GREEN);
		//}
		if ((arrayMap[x][y] & 4) == ArrayMap.TYPE_WALL_LEFT) { // = 4

			StateGameplay.spriteWall.drawAFrame(c, 3, y * MAP_CELL_WIDTH - 3, x * MAP_CELL_HEIGHT);
			//	MainActivity.mainPaint.setColor(Color.YELLOW);
			//	c.drawRect(y * MAP_CELL_WIDTH - 3, x * MAP_CELL_HEIGHT, y * MAP_CELL_WIDTH + 3, x * MAP_CELL_HEIGHT + MAP_CELL_HEIGHT, MainActivity.mainPaint);
			//	MainActivity.mainPaint.setColor(Color.GREEN);
		}
		//	if ((arrayMap[x][y] & 8) == ArrayMap.TYPE_WALL_RIGHT) { // = 8
		//	StateGameplay.spriteWall.drawAFrame(c, 17,(y + 1) * MAP_CELL_WIDTH - 3,x * MAP_CELL_HEIGHT);
		//	MainActivity.mainPaint.setColor(Color.YELLOW);
		//	c.drawRect((y + 1) * MAP_CELL_WIDTH - 3, x * MAP_CELL_HEIGHT, (y + 1) * MAP_CELL_WIDTH + 3, x * MAP_CELL_HEIGHT + MAP_CELL_HEIGHT, MainActivity.mainPaint);
		//	MainActivity.mainPaint.setColor(Color.GREEN);
		//	}
	}

	public void drawDoorExit(Canvas c)
	{
		if (doortype == ArrayMap.TYPE_EXIT_TOP) { // = 1
			MainActivity.mainPaint.setColor(Color.RED);
			c.drawRect(doorExit_Col * MAP_CELL_WIDTH, doorExit_Row * MAP_CELL_HEIGHT - 3, doorExit_Col * MAP_CELL_WIDTH + MAP_CELL_WIDTH, doorExit_Row * MAP_CELL_HEIGHT + 3, MainActivity.mainPaint);
			c.drawLine(doorExit_Col * MAP_CELL_WIDTH, doorExit_Row * MAP_CELL_HEIGHT - 3, doorExit_Col * MAP_CELL_WIDTH + MAP_CELL_WIDTH, doorExit_Row * MAP_CELL_HEIGHT + 3, MainActivity.mainPaint);
			MainActivity.mainPaint.setColor(Color.GREEN);
		}
		if (doortype == ArrayMap.TYPE_EXIT_BOTTOM) { // = 2
			MainActivity.mainPaint.setColor(Color.RED);
			c.drawRect(doorExit_Col * MAP_CELL_WIDTH, (doorExit_Row + 1) * MAP_CELL_HEIGHT - 3, doorExit_Col * MAP_CELL_WIDTH + MAP_CELL_WIDTH, (doorExit_Row + 1) * MAP_CELL_HEIGHT + 3, MainActivity.mainPaint);
			c.drawLine(doorExit_Col * MAP_CELL_WIDTH, (doorExit_Row + 1) * MAP_CELL_HEIGHT - 3, doorExit_Col * MAP_CELL_WIDTH + MAP_CELL_WIDTH, (doorExit_Row + 1) * MAP_CELL_HEIGHT + 3, MainActivity.mainPaint);
			MainActivity.mainPaint.setColor(Color.GREEN);
		}
		if (doortype == ArrayMap.TYPE_EXIT_LEFT) { // = 4
			MainActivity.mainPaint.setColor(Color.RED);
			c.drawRect(doorExit_Col * MAP_CELL_WIDTH - 3, doorExit_Row * MAP_CELL_HEIGHT, doorExit_Col * MAP_CELL_WIDTH + 3, doorExit_Row * MAP_CELL_HEIGHT + MAP_CELL_HEIGHT, MainActivity.mainPaint);
			c.drawLine(doorExit_Col * MAP_CELL_WIDTH - 3, doorExit_Row * MAP_CELL_HEIGHT, doorExit_Col * MAP_CELL_WIDTH + 3, doorExit_Row * MAP_CELL_HEIGHT + MAP_CELL_HEIGHT, MainActivity.mainPaint);
			MainActivity.mainPaint.setColor(Color.GREEN);
		}
		if (doortype == ArrayMap.TYPE_EXIT_RIGHT) { // = 8
			MainActivity.mainPaint.setColor(Color.RED);
			c.drawRect((doorExit_Col + 1) * MAP_CELL_WIDTH - 3, doorExit_Row * MAP_CELL_HEIGHT, (doorExit_Col + 1) * MAP_CELL_WIDTH + 3, doorExit_Row * MAP_CELL_HEIGHT + MAP_CELL_HEIGHT, MainActivity.mainPaint);
			c.drawLine((doorExit_Col + 1) * MAP_CELL_WIDTH - 3, doorExit_Row * MAP_CELL_HEIGHT, (doorExit_Col + 1) * MAP_CELL_WIDTH + 3, doorExit_Row * MAP_CELL_HEIGHT + MAP_CELL_HEIGHT, MainActivity.mainPaint);
			MainActivity.mainPaint.setColor(Color.GREEN);
		}
		StateGameplay.spriteDPad.drawAnim(c);
	}

	public void setDoorExitArrow(int x, int y)
	{
		if ((arrayMap[x][y] & ArrayMap.TYPE_EXIT_TOP) == ArrayMap.TYPE_EXIT_TOP) { // = 1
			doorExit_Col = y;
			doorExit_Row = x;
			doortype = ArrayMap.TYPE_EXIT_TOP;
			StateGameplay.spriteDPad.setAnim(0, y * MAP_CELL_WIDTH + MAP_CELL_WIDTH/2, x * MAP_CELL_HEIGHT + MAP_CELL_HEIGHT/2, true, false);
		}
		if ((arrayMap[x][y] & ArrayMap.TYPE_EXIT_BOTTOM) == ArrayMap.TYPE_EXIT_BOTTOM) { // = 2
			doorExit_Col = y;
			doorExit_Row = x;
			doortype = ArrayMap.TYPE_EXIT_BOTTOM;
			StateGameplay.spriteDPad.setAnim(1, y * MAP_CELL_WIDTH + MAP_CELL_WIDTH/2, x * MAP_CELL_HEIGHT + MAP_CELL_HEIGHT/2, true, false);
		}
		if ((arrayMap[x][y] & ArrayMap.TYPE_EXIT_LEFT) == ArrayMap.TYPE_EXIT_LEFT) { // = 4
			doorExit_Col = y;
			doorExit_Row = x;
			doortype = ArrayMap.TYPE_EXIT_LEFT;
			StateGameplay.spriteDPad.setAnim(2, y * MAP_CELL_WIDTH + MAP_CELL_WIDTH/2, x * MAP_CELL_HEIGHT + MAP_CELL_HEIGHT/2, true, false);
		}
		if ((arrayMap[x][y] & ArrayMap.TYPE_EXIT_RIGHT) == ArrayMap.TYPE_EXIT_RIGHT) { // = 8
			doorExit_Col = y;
			doorExit_Row = x;
			doortype = ArrayMap.TYPE_EXIT_RIGHT;
			StateGameplay.spriteDPad.setAnim(3, y * MAP_CELL_WIDTH + MAP_CELL_WIDTH/2, x * MAP_CELL_HEIGHT + MAP_CELL_HEIGHT/2, true, false);
		}

	}

	public Map(int level)
	{
		selectMap(level);
		for (int i = 0; i < Map.MAP_ROWS; i++)
			for (int j = 0; j < Map.MAP_COLUMNS; j++) {
				setDoorExitArrow(i, j);
				if ((arrayMap[i][j] >> 12) == 6) {//explorer
					StateGameplay.explorer.setPos(j, i);
					//	Log.d("aa", "aa");
				} else if ((arrayMap[i][j] >> 12) == 1) { // TYPE_MUMMY_WHITE =
															// 4096;//001000000000000
															// (if >>12 = 001 =1)
					StateGameplay.creature.add(new Creature(null, i, j, 20, 20, Creature.TYPE_WHILE));

				} else if ((arrayMap[i][j] >> 12) == 2) { // TYPE_MUMMY_RED = 8192;//
															// 010000000000000 (if >>12
															// = 010 =2)
					StateGameplay.creature.add(new Creature(null, i, j, 20, 20, Creature.TYPE_RED));

				}
			}
	}

	public boolean checkWallCollition(int directtion, int currentx, int currenty)
	{
		//directtion =  TYPE_WALL_TOP = 1; //(1<<0)
		// TYPE_WALL_BOTTOM = 2;//(1<<1)
		// TYPE_WALL_LEFT = 4;//(1)
		// TYPE_WALL_RIGHT = 8;	//(1<<2)

		if ((currenty == 0 && directtion == ArrayMap.DIRECTION_UP) || ((currenty == MAP_ROWS - 1) && directtion == ArrayMap.DIRECTION_DOWN) || (currentx == 0 && directtion == ArrayMap.DIRECTION_LEFT) || ((currentx == MAP_COLUMNS - 1) && directtion == ArrayMap.DIRECTION_RIGHT))
			return true;

		if ((directtion == ArrayMap.DIRECTION_UP && ((arrayMap[currenty][currentx] & ArrayMap.TYPE_WALL_TOP) == ArrayMap.TYPE_WALL_TOP)) || (directtion == ArrayMap.DIRECTION_DOWN && ((arrayMap[currenty][currentx] & ArrayMap.TYPE_WALL_BOTTOM) == ArrayMap.TYPE_WALL_BOTTOM)) || (directtion == ArrayMap.DIRECTION_LEFT && ((arrayMap[currenty][currentx] & ArrayMap.TYPE_WALL_LEFT) == ArrayMap.TYPE_WALL_LEFT)) || (directtion == ArrayMap.DIRECTION_RIGHT && ((arrayMap[currenty][currentx] & ArrayMap.TYPE_WALL_RIGHT) == ArrayMap.TYPE_WALL_RIGHT)))
			return true;

		return false;
	}

	public boolean isStandAtExit(int m_pos_cols, int m_pos_rows)
	{
		
		if ((arrayMap[m_pos_rows][m_pos_cols] & ArrayMap.TYPE_EXIT_RIGHT) == ArrayMap.TYPE_EXIT_RIGHT || (arrayMap[m_pos_rows][m_pos_cols] & ArrayMap.TYPE_EXIT_LEFT) == ArrayMap.TYPE_EXIT_LEFT || (arrayMap[m_pos_rows][m_pos_cols] & ArrayMap.TYPE_EXIT_TOP) == ArrayMap.TYPE_EXIT_TOP || (arrayMap[m_pos_rows][m_pos_cols] & ArrayMap.TYPE_EXIT_BOTTOM) == ArrayMap.TYPE_EXIT_BOTTOM) {
			return true;
		}
		return false;
	}

	public void selectMap(int level)
	{
		switch (level)
		{
		case 1:
			arrayMap = ArrayMap.Stage1;
			break;
		case 2:
			arrayMap = ArrayMap.Stage2;
			break;
		case 3:
			arrayMap = ArrayMap.Stage3;
			break;
		case 4:
			arrayMap = ArrayMap.Stage4;
			break;
		case 5:
			arrayMap = ArrayMap.Stage5;
			break;
		case 6:
			arrayMap = ArrayMap.Stage6;
			break;
		case 7:
			arrayMap = ArrayMap.Stage7;
			break;
		case 8:
			arrayMap = ArrayMap.Stage8;
			break;
		case 9:
			arrayMap = ArrayMap.Stage9;
			break;
		case 10:
			arrayMap = ArrayMap.Stage10;
			break;
		case 11:
			arrayMap = ArrayMap.Stage11;
			break;
		case 12:
			arrayMap = ArrayMap.Stage12;
			break;
		case 13:
			arrayMap = ArrayMap.Stage13;
			break;
		case 14:
			arrayMap = ArrayMap.Stage14;
			break;
		case 15:
			arrayMap = ArrayMap.Stage15;
			break;
		case 16:
			arrayMap = ArrayMap.Stage16;
			break;
		case 17:
			arrayMap = ArrayMap.Stage17;
			break;
		case 18:
			arrayMap = ArrayMap.Stage18;
			break;
		case 19:
			arrayMap = ArrayMap.Stage19;
			break;
		case 20:
			arrayMap = ArrayMap.Stage20;
			break;
		case 21:
			arrayMap = ArrayMap.Stage21;
			break;
		case 22:
			arrayMap = ArrayMap.Stage22;
			break;
		case 23:
			arrayMap = ArrayMap.Stage23;
			break;
		case 24:
			arrayMap = ArrayMap.Stage24;
			break;
		case 25:
			arrayMap = ArrayMap.Stage25;
			break;
		case 26:
			arrayMap = ArrayMap.Stage26;
			break;
		case 27:
			arrayMap = ArrayMap.Stage27;
			break;
		case 28:
			arrayMap = ArrayMap.Stage28;
			break;
		case 29:
			arrayMap = ArrayMap.Stage29;
			break;
		case 30:
			arrayMap = ArrayMap.Stage30;
			break;
		case 31:
			arrayMap = ArrayMap.Stage31;
			break;
		case 32:
			arrayMap = ArrayMap.Stage32;
			break;
		case 33:
			arrayMap = ArrayMap.Stage33;
			break;
		case 34:
			arrayMap = ArrayMap.Stage34;
			break;
		case 35:
			arrayMap = ArrayMap.Stage35;
			break;
		case 36:
			arrayMap = ArrayMap.Stage36;
			break;
		case 37:
			arrayMap = ArrayMap.Stage37;
			break;
		case 38:
			arrayMap = ArrayMap.Stage38;
			break;
		case 39:
			arrayMap = ArrayMap.Stage39;
			break;
		case 40:
			arrayMap = ArrayMap.Stage40;
			break;
		case 41:
			arrayMap = ArrayMap.Stage41;
			break;
		case 42:
			arrayMap = ArrayMap.Stage42;
			break;
		case 43:
			arrayMap = ArrayMap.Stage43;
			break;
		case 44:
			arrayMap = ArrayMap.Stage44;
			break;
		case 45:
			arrayMap = ArrayMap.Stage45;
			break;
		case 46:
			arrayMap = ArrayMap.Stage46;
			break;
		case 47:
			arrayMap = ArrayMap.Stage47;
			break;
		case 48:
			arrayMap = ArrayMap.Stage48;
			break;
		case 49:
			arrayMap = ArrayMap.Stage49;
			break;
		case 50:
			arrayMap = ArrayMap.Stage50;
			break;
		case 51:
			arrayMap = ArrayMap.Stage51;
			break;
		case 52:
			arrayMap = ArrayMap.Stage52;
			break;
		case 53:
			arrayMap = ArrayMap.Stage53;
			break;
		case 54:
			arrayMap = ArrayMap.Stage54;
			break;
		case 55:
			arrayMap = ArrayMap.Stage55;
			break;
		case 56:
			arrayMap = ArrayMap.Stage56;
			break;
		case 57:
			arrayMap = ArrayMap.Stage57;
			break;
		case 58:
			arrayMap = ArrayMap.Stage58;
			break;
		case 59:
			arrayMap = ArrayMap.Stage59;
			break;
		case 60:
			arrayMap = ArrayMap.Stage60;
			break;
		default:
			break;
		}
	}

}
